using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.Rss;
using NTTHAO.CommonWeb;

public partial class admin_webparts_Plug_ins_RssNews_RssMenuInput : System.Web.UI.UserControl
{
    public RssPageGroup _Group = null;
    public RssMenu _Menu = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave.Text = this.btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);

        int siteID = WebGlobal.GetSite().ID;
        int langID = LanguageUtility.GetLanguageID();
        try
        {
            _Group = new RssPageGroup(Convert.ToInt32(Request.QueryString["g"].Trim()));
            int menuID = Convert.ToInt32(Request.QueryString["m"].Trim());
            if (menuID > 0)
            {
                _Menu = new RssMenu(menuID);
            }
            if (_Group.ID < 1 || (_Menu != null && _Menu.ID < 1))
            {
                throw new Exception("This function is invalid.");
            }

            if ("editmenu".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase)
                && _Menu != null
                && !this.IsPostBack)
            {
                txtMenuText.Text = _Menu.Text;
                chkActive.Checked = _Menu.Activated;
            }
        }
        catch //(Exception ex)
        {
            Response.Redirect("?type=invalidurl&ReturnUrl=" + Server.UrlEncode(Request.RawUrl), false);
            return;
            //HtmlGenericControl ctrl = new HtmlGenericControl("div");
            //ctrl.InnerHtml = "<p>" + ex.Message + "</p><p>" + ex.StackTrace + "</p>";
            //this.Controls.Add(ctrl);
        }
    }

    public void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            RssMenu menu = null;
            if ("addmenu".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                menu = new RssMenu();
                menu.ParentID = _Menu != null ? _Menu.ID : 0;
                menu.RssGroupID = _Group.ID;
            }
            else if ("editmenu".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                menu = new RssMenu(Convert.ToInt32(Request.QueryString["m"].Trim()));
            }

            menu.Text = txtMenuText.Text.Trim();
            menu.Activated = chkActive.Checked;

            if ("addmenu".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                menu.Insert();
            }
            else
            {
                menu.Update();
            }

            string bUrl = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&item=" + Request.QueryString["item"] + "&g=" + Request.QueryString["g"];
            Response.Redirect(bUrl, false);
            return;
        }
        catch (Exception ex)
        {
            HtmlGenericControl ctrl = new HtmlGenericControl("div");
            ctrl.InnerHtml = "<p>" + ex.Message + "</p><p>" + ex.StackTrace + "</p>";
            this.Controls.Add(ctrl);
        }
    }
}
